package ldkxingzhe.ldkxingzhedemo.service;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by ke on 15-9-9.
 */
public class AsyncTaskActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = AsyncTaskActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        TextView textView = new TextView(this);
        textView.setText("Simple AsyncTask Test Activity");
        layout.addView(textView);

        Button button = new Button(this);
        button.setText("Click start AsyncTaskA");
        layout.addView(button);
        button.setOnClickListener(this);
        setContentView(layout);
    }

    @Override
    public void onClick(View v) {
        LogDemo.v(TAG, "onClick");
        new MyLogAsyncTask(this).execute();
/*        try {
            LogDemo.v(TAG,"main thread is going to sleep five seconds");
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        new MyLogAsyncTask(this).execute();
    }

    class MyLogAsyncTask extends AsyncTask<String,Integer,Integer>{

        private ProgressDialog progressDialog;
        private Context context;
        public MyLogAsyncTask(Context context){
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showLog("onPreExecute");
            progressDialog = new ProgressDialog(context,ProgressDialog.STYLE_SPINNER);
            progressDialog.setMax(5);
            progressDialog.setTitle("title");
            progressDialog.setCancelable(false);
            progressDialog.setMessage("progress");
            progressDialog.setIndeterminate(false);
            progressDialog.show();
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            showLog("onPostExecute");

            progressDialog.dismiss();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            showLog("onProgressUpdate");
            for(Integer integer : values){
                LogDemo.v(TAG,"Integer = " + integer);
            }
            progressDialog.setProgress(values[0]);
        }

        @Override
        protected Integer doInBackground(String... params) {
            showLog("doInBackground");
            for(int i = 0; i < 5; i++){
                try {
                    Thread.sleep(1000);
                    LogDemo.v(TAG,"doInBackground, and i = " + i);
                    publishProgress(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        private void showLog(String method){
            LogDemo.v(TAG,"method = " + method + ", thread = " + Thread.currentThread());
        }
    }
}
